package com.zuo.mall.controller;

import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.common.api.CommonResult;
import com.zuo.mall.dto.PmsProductAttributeCategoryItem;
import com.zuo.mall.entity.PmsProductAttributeCategory;
import com.zuo.mall.service.PmsProductAttributeCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("分类属性")
@RestController
@RequestMapping("productAttribute/category")
public class PmsProductAttributeCategoryController {
    @Autowired
    private PmsProductAttributeCategoryService productAttributeCategoryService;
    @ApiOperation("添加新的商品属性")
    @PostMapping("create")
    private CommonResult createItem(String name) {
        int count = productAttributeCategoryService.createNew(name);
        if (count >0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
    @GetMapping("/list/withAttr")
    public CommonResult<List<PmsProductAttributeCategoryItem>> getListWithAttr() {
        List<PmsProductAttributeCategoryItem> productAttributeCategoryItems = productAttributeCategoryService.getListWithAttr();
        return CommonResult.success(productAttributeCategoryItems);
    }

    @ApiOperation("分页获取所有商品属性分类")
    @GetMapping("/list")
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getList
            (@RequestParam(defaultValue = "5") Integer pageSize,
             @RequestParam(defaultValue = "1") Integer pageNum) {
        CommonPage<PmsProductAttributeCategory> productAttributeCategoryCommonPage =
                productAttributeCategoryService.getList(pageSize, pageNum);
        return CommonResult.success(productAttributeCategoryCommonPage);
    }

    @PostMapping("update/{id}")
    public CommonResult update(@PathVariable("id")Long id,
                               String name) {
        int count = productAttributeCategoryService.update(id, name);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @GetMapping("delete/{id}")
    public CommonResult delete(@PathVariable("id")Long id) {
        int count = productAttributeCategoryService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

}
